package cn.virens.cmpt.rabbit.packet;

import java.io.Serializable;
import java.nio.charset.Charset;

import com.alibaba.fastjson.JSON;

import cn.virens.common.ObjectUtil;
import cn.virens.common.StringUtil;

public abstract class Packet implements Serializable {
	private static final long serialVersionUID = 9204932968129541278L;

	private String address;

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public abstract String cmd();

	@Override
	public int hashCode() {
		return ObjectUtil.hash(address);
	}

	@Override
	public boolean equals(Object obj) {
		return ObjectUtil.equals(obj, this);
	}

	/** 指令包序列化 */
	public byte[] serialize(Charset charset) {
		return StringUtil.bytes(toString(), charset);
	}

	@Override
	public String toString() {
		return JSON.toJSONString(this);
	}
}
